package com.xzz.directprint.service;

import com.aspose.cad.Image;
import com.aspose.cad.ImageOptionsBase;
import com.aspose.cad.SizeF;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.JpegOptions;
import com.aspose.cad.imageoptions.UnitType;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Author: zhanglm
 * @CreateDate: 2024-12-03 15:25
 * @FullNameClass: com.xzz.directprint.service.Dwg2Jpg
 * @Description:
 */
public class CAD2JPGService {


    // Define the working directory path.
    @Value("${pdf.path}")
    private String dataDir;



    public void Cad2Pic(String fileName){

        // Call the Load method of the Image class to load the source DWG file.
        Image sourceImage = Image.load(dataDir+fileName);
        // Create an object of the JpegOptions for the resultant image and assign it to the instance of the ImageOptionsBase class.
        ImageOptionsBase jpegOptions = new JpegOptions();
        // Instantiate an instance of the CadRasterizationOptions to enable CAD rasterization options.
        CadRasterizationOptions rasterOptions = new CadRasterizationOptions();
        // The setEmbedBackground method will set whether the background color is not equal to the default background color of the output format.
        rasterOptions.setEmbedBackground(true);
        // Invoke the setPageWidth method to set page width.
        SizeF sizeF = new SizeF();
        rasterOptions.setUnitType(UnitType.Centimenter);
        rasterOptions.setPageWidth(210);
        // Call the setPageHeight method to set the height of the page.
        rasterOptions.setPageHeight(297);
        // Invoke the setVectorRasterizationOptions method to define rasterization options.
        jpegOptions.setVectorRasterizationOptions(rasterOptions);
        // The save method will save the resultant JPG image on the disk.
        //TODO 修改文件名，添加逻辑
        sourceImage.save(dataDir + "dwg-to-jpg.jpg", jpegOptions);
    }





    // Convert DWG to JPG in Java using AutoCAD Library
    public static void main(String[] args) throws Exception {

// Call the Load method of the Image class to load the source DWG file.
        Image sourceImage = Image.load("D:/tuzhi/"+"autocad.dxf");
// Create an object of the JpegOptions for the resultant image and assign it to the instance of the ImageOptionsBase class.
        ImageOptionsBase jpegOptions = new JpegOptions();
// Instantiate an instance of the CadRasterizationOptions to enable CAD rasterization options.
        CadRasterizationOptions rasterOptions = new CadRasterizationOptions();
// The setEmbedBackground method will set whether the background color is not equal to the default background color of the output format.
        rasterOptions.setEmbedBackground(true);
// Invoke the setPageWidth method to set page width.
        rasterOptions.setPageWidth(900);
// Call the setPageHeight method to set the height of the page.
        rasterOptions.setPageHeight(900);
// Invoke the setVectorRasterizationOptions method to define rasterization options.
        jpegOptions.setVectorRasterizationOptions(rasterOptions);
// The save method will save the resultant JPG image on the disk.
        sourceImage.save("D:/tuzhi/" + "dwg-to-jpg.jpg", jpegOptions);
    }
}
